import Cookies from "js-cookie";
const prefix = "im_operate_backstage_";
export const CookiesStorage = {
  getItem: (key: string) => {
    if (!Cookies.get(prefix + key)) {
      return "";
    }
    return decodeByBase64(Cookies.get(prefix + key) as string);
  },
  setItem: (key: string, value: any) => {
    const duration = 8 * 60 * 60 * 1000; // token过期时间
    const expires = new Date(Date.now() + duration);
    Cookies.set(prefix + key, encodeByBase64(value), { expires });
  },
  removeItem: (key: string) => {
    Cookies.remove(prefix + key);
  },
} as Storage;
export const SessionStorage: Storage = {
  getItem: (key: string) => {
    if (!sessionStorage.getItem(prefix + key)) return "";
    return decodeByBase64(sessionStorage.getItem(prefix + key) as string);
  },
  setItem: (key: string, value: any) => {
    sessionStorage.setItem(prefix + key, encodeByBase64(value));
  },
  removeItem: (key: string) => {
    sessionStorage.removeItem(prefix + key);
  },
  clear() {
    sessionStorage.clear();
  },
  key: (index: number) => {
    return sessionStorage.key(index);
  },
  length: sessionStorage.length,
};
export const LocalStorage: Storage = {
  getItem: (key: string) => {
    if (!localStorage.getItem(prefix + key)) {
      return "";
    }
    return decodeByBase64(localStorage.getItem(prefix + key) as string);
  },
  setItem: (key: string, value: any) => {
    localStorage.setItem(prefix + key, encodeByBase64(value));
  },
  removeItem: (key: string) => {
    localStorage.removeItem(prefix + key);
  },
  clear() {
    localStorage.clear();
  },
  key: (index: number) => {
    return localStorage.key(index);
  },
  length: localStorage.length,
};
